/*
 * 太原科技大学。
 * Copyright 2021, YCHS Information&Science Technology Co.,Ltd,
 * All rights reserved.
 */
package com.example.demo.Utils;


import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author liuchao
 * @version 1.0
 * @date 2024/9/13
 */

@Component
public class HandleUtil {

    //前端请求信息处理,返回的数组第一项为User-Agent,第二项为请求IP,第三项为Phone
    public  String[] requestMessage(HttpServletRequest request){
        //信息缓存数组
        String[] message = new String[3];
        //获取请求的用户信息
        message[0]=request.getHeader("User-Agent");
        //获取请求的IP
        message[1]=request.getHeader("X-Forwarded-For");

        message[2]=request.getHeader("Phone");

        return (message);
    }
    //随机验证码生成
    public  String getCode(int length) {
        StringBuilder code = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int choice = random.nextInt(3);
            if (choice == 0) {
                // 数字
                code.append(random.nextInt(10));
            } else if (choice == 1) {
                // 大写字母
                code.append((char) (random.nextInt(26) + 65));
            } else {
                // 小写字母
                code.append((char) (random.nextInt(26) + 97));
            }
        }
        return code.toString();
    }
    //验证码缓存
    public  void redisSet(String message, Object Handler, RedisTemplate redisTemplate){
        redisTemplate.opsForValue().set(message,Handler,10, TimeUnit.MINUTES);
    }
    //验证码验证
    public  boolean redisGet(String message,String Handler,RedisTemplate redisTemplate){
        String tmp=(String) redisTemplate.opsForValue().get(message);

        //验证成功
        if(Handler.equals(tmp)){
            return true;
        }else{
            return false;
        }
    }

}
